#ifndef __SUBS_H
#define __SUBS_H

#include "Output.h"
#include "Directories.h"
#include <time.h>
#include <stdbool.h>

#define FILEPATTERN_SUBBOARD		"sub-%05u"
#define FILEPATTERN_THREAD			"thrd-%04X-%04X"
#define FILEPATTERN_MSG_IDX_REC		"idx-%04X%04X%04X"
#define FILEPATTERN_MSG				"msg-%04X%04X%04X"
#define FILEPATTERN_MSG_BODY		"bdy-%04X%04X%04X"

typedef struct Bases
{
	char subboardCount;
} bases_t;

extern bases_t _currentBases;

typedef struct SubboardIndexRecord
{
	char subboardNumber;
	char name[21];
	char description[161];
} subIndexRec_t;

typedef struct Subboard
{
	char subboardNumber;
	char name[21];
	char description[161];
	char minSecurityLevelRead;
	char minSecurityLevelPost;
	unsigned int accessGroupRead;
	unsigned int accessGroupPost;
	int sigopUsernumber;
	unsigned int lowThreadNumber;
	unsigned int highThreadNumber;
	unsigned int maxThreadCount;	// 0 means unlimited
	dirInfo_t directory;
	bool isDeleted;
	bool isLocked;
} subboard_t;

extern subboard_t _currentSubboard;

typedef struct MessageThread
{
	unsigned int threadNumber;
	char topic[71];
	time_t startedOn;
	int startedByUsernumber;
	char startedByAlias[21];
	unsigned int highMessageNumber;
	bool isDeleted;
	bool isLocked;
	char keywords[5][21];
} msgThread_t;

extern msgThread_t _currentMsgThread;

typedef struct Message
{
	unsigned int messageNumber;
	char subject[71];
	time_t postedOn;
	int postedByUsernumber;
	int respondingToUsernumber;
	int responseToMessageNumber;
	encoding_t originalEncoding;
	unsigned int length;
	bool isDeleted;
	bool isPrivate;
	char keywords[5][21];
} message_t;

extern message_t _currentMsg;

typedef enum MessageStatuses
{
	MS_NORMAL, MS_DELETED
} msgStatuses_t;

typedef struct MessageStatus
{
	msgStatuses_t status;
	time_t lastUpdatedOn;
	int	lastUpdatedBy;
} msgStatus_t;

typedef struct UserMessageIndexRecord
{
	char subboardNumber;
	unsigned int threadNumber;
	unsigned int lastMessageNumber;
} userMsgIdxRec_t;

extern userMsgIdxRec_t _currentUserMsgIdx;

void enterSubs(void);
void editSubs(void);
void newMessageScan(void);

unsigned int getUnreadCount(void);
unsigned int getUnreadMessageCount(void);
void loadCurrents(void);
void chooseThread(void);
void createThread(void);
void saveCurrentThread(void);
void createMessage(bool reply);
void loadThread(int threadNumber, bool loadIndexRecord, bool showIntro);
void listThreads(void);
void listSubs(void);
void readMessageByNumber(void);
void readMessage(int subboardId, unsigned int threadId, unsigned int messageId);
bool saveMessage(message_t *message, char *body);
void findByKeyword(char *keyword);
void gotoSubboard(void);
void loadSubboard(int subboardNumber);
void displayMenu(void);
void displayPrompt(void);
void reply(void);
void deleteCurrentMessage(void);


#endif